package com.ruoyi.project.module.applicationProperty.controller;

import java.util.List;

import com.ruoyi.project.module.application.service.IApplicationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.applicationProperty.domain.ApplicationProperty;
import com.ruoyi.project.module.applicationProperty.service.IApplicationPropertyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

import javax.annotation.Resource;

/**
 * 应用属性 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-25
 */
@Controller
@RequestMapping("/module/applicationProperty")
public class ApplicationPropertyController extends BaseController
{
    private String prefix = "module/applicationProperty";
	
	@Autowired
	private IApplicationPropertyService applicationPropertyService;

	@Resource
	private IApplicationService applicationService;
	
	@RequiresPermissions("module:applicationProperty:view")
	@GetMapping()
	public String applicationProperty()
	{
	    return prefix + "/applicationProperty";
	}

	/**
	 * 查询应用属性列表
	 */
	@RequiresPermissions("module:applicationProperty:list")
	@PostMapping("/listtree/{uniqueId}")
	@ResponseBody
	public List<ApplicationProperty> listtree(@PathVariable("uniqueId") Long uniqueId,ApplicationProperty applicationProperty)
	{
		applicationProperty.setConfigApplicationId(uniqueId);
		List<ApplicationProperty> list = applicationPropertyService.selectApplicationPropertyList(applicationProperty);
		return (list);
	}
	/**
	 * 查询应用属性列表
	 */
	@RequiresPermissions("module:applicationProperty:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ApplicationProperty applicationProperty)
	{
		startPage();
        List<ApplicationProperty> list = applicationPropertyService.selectApplicationPropertyList(applicationProperty);
		return getDataTable(list);
	}
	
	/**
	 * 新增应用属性
	 */
	@GetMapping("/selectGroup/{configApplicationId}/{parentId}")
	public String selectGroup(@PathVariable("configApplicationId") Long configApplicationId,
					  @PathVariable(value = "parentId",required = false) Long parentId,
					  ModelMap mmap)
	{
		mmap.put("configApplicationId", configApplicationId);
	    return prefix + "/selectGroup";
	}
	
	/**
	 * 新增保存应用属性
	 */
	@RequiresPermissions("module:applicationProperty:add")
	@Log(title = "应用属性", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ApplicationProperty applicationProperty)
	{		
		return toAjax(applicationPropertyService.insertApplicationProperty(applicationProperty));
	}

	/**
	 * 修改应用属性
	 */
	@GetMapping("/edit/{configApplicationPropertyId}")
	public String edit(@PathVariable("configApplicationPropertyId") Long configApplicationPropertyId, ModelMap mmap)
	{
		ApplicationProperty applicationProperty = applicationPropertyService.selectApplicationPropertyById(configApplicationPropertyId);
		mmap.put("applicationProperty", applicationProperty);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存应用属性
	 */
	@RequiresPermissions("module:applicationProperty:edit")
	@Log(title = "应用属性", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ApplicationProperty applicationProperty)
	{		
		return toAjax(applicationPropertyService.updateApplicationProperty(applicationProperty));
	}
	
	/**
	 * 删除应用属性
	 */
	@RequiresPermissions("module:applicationProperty:remove")
	@Log(title = "应用属性", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(applicationPropertyService.deleteApplicationPropertyByIds(ids));
	}

	@PostMapping("/hasChilds")
	@ResponseBody
	public Boolean hasChilds(Long uniqueId){
		return applicationPropertyService.hasChilds(uniqueId);
	}

	/**
	 * 发布配置组
	 */
	@RequiresPermissions("module:applicationProperty:release")
	@Log(title = "应用属性发布", businessType = BusinessType.OTHER)
	@PostMapping( "/release")
	@ResponseBody
	@Transactional
	public AjaxResult release(String ids)
	{
		if( applicationService.release(ids)){
		    return toAjax(1);
		}
		return toAjax(0);
	}
}
